
class.save_ui = extends(class.panel){

    fdf = 'tooltip_backdrop1',
    name = '游戏存档',
    show_flag = false,
    init = function (self)

        class.panel.init(self)
        --标题
        self.title = self:add_text(self.name,0,0,self.w,40,16,5)
        --目录区域
        self.map = self:add_panel('',10,50,130,430)
        self.map.is_scroll = true
        self.map:set_view_port(true)
        --显示区域
        self.show_panel = self:add_panel('',150,50,self.w - 200,self.h - 100)
        if self.icon_param then
            self:init_icon(self.icon_param)
        end
        --翻页
        local w0,h0 = 21,26
        local w1 = 80
        local x0 = (self.show_panel.w - 2*w0 - w1)/2
        local y0 = self.show_panel.h + 5
        local art1 = [[ui\上一页按钮.tga]]
        local art2 = [[ui\下一页按钮.tga]]
        self.page_los = self.show_panel:add_button(art1,x0,y0,w0,h0)
        self.page_add = self.show_panel:add_button(art2,x0+w0+w1,y0,w0,h0)
        self.page_text = self.show_panel:add_text('1/1',x0+w0,y0,w1,h0,10,5)
        self.page = 1
        self.max_page = 1

        --esc
        local button = self:add_button([[ui\关闭1.tga]],self.w-64,0,64,64)
        button.hover_image = [[ui\关闭2.tga]]
        button.keys = {'ESC'}
        self.esc = button
        function button:on_button_key_down()
            self:notify('on_button_clicked',self)
        end
        
        return self
    end,

    new = function (parent,name,x,y,w,h,icon_param)
        local panel = class.save_ui:builder 
        {
            parent = parent,
            name = name,
            x = x,
            y = y,
            w = w,
            h = h,
            icon_param = icon_param,
        }
        return panel
    end,
    --初始化显示格子 数值x,数值y 是长宽
    icon_list = {},
    init_icon = function(self,param)
        local ui = self.show_panel
        local w,h = param.w,param.h
        local a,b = param.a,param.b
        local g1 = param.g1 or 0
        local g2 = param.g2 or 0
        local x1 = param.x1 or 0
        local y1 = param.y1 or 0
        if a==nil then
            a = math.floor(ui.w/w)
            g1 = (ui.w - a*w - 2*x1)/(a-1)
        end
        if b==nil then
            b = math.floor(ui.h/h)
            g2 = (ui.h - b*h - 2*y1)/(b-1)
        end
        if w==nil then
            w = (ui.w - (a-1)*g1 - 2*x1)/a
        end
        if h==nil then
            h = (ui.h - (b-1)*g2 - 2*y1)/b
        end
        for _,button in ipairs(self.icon_list) do
            button:destroy()
        end
        self.icon_list = {}
        for b1=1,b do
            for a1=1,a do
                local button = ui:add_button([[ui\ui_b0.tga]],(a1-1)*(w+g1) + x1,(b1-1)*(h+g2) + y1,w,h)
                button.icon = button:add_panel('')
                button.count = button:add_text('',button.w*2/3,button.h*2/3,button.w/3-2,button.h/3-2,12,5)
                button.back = button:add_panel([[textures\black32.blp]])
                button.back:set_alpha(0.7)
                button.class = '存档按钮'
                table.insert(self.icon_list,button)
            end
        end
    end,
    --添加类型
    --类型名，绑定数据
    map_list = {},
    add_map = function(self,name,data)
        local index = #self.map_list
        local button = class.button:builder{
            parent = self.map,
            y = index*50,
            h = 50,
            index = index,
            class = '菜单按钮',
            back = {
                type = 'panel',
                --fdf = 'dialoguebox_background',
                fdf = 'tooltip_backdrop1',
                level = 1,
            },
            title = {
                type = 'text',
                text = name,
                align = 5,
                font_size = 12,
                level = 2,
            }
        }
        button.map_data = data
        table.insert(self.map_list,button)
    end,
    update = function(self,data)
        local player = ac.player.self
        local data = self.show_data or {}
        local max = #self.icon_list
        local index = (self.page - 1)*max
        self.max_page = math.max(math.ceil(#data/max),1)
        self.page_text:set_text(('%s/%s'):format(self.page,self.max_page))
        for a,button in ipairs(self.icon_list) do
            local skill = data[index + a]
            button.skill = skill
            if skill then
                local open = skill.open or player:get_data('激活-'..skill.name)
                button.icon:set_normal_image(skill.art)
                button.count:set_text(skill.count or '')
                if open then
                    button.back:hide()
                else
                    button.back:show()
                end
            else
                button.count:set_text('')
                button.icon:set_normal_image('')
                button.back:hide()
            end
        end
    end,
    on_button_clicked = function(self,button)
        local player = ac.player.self
        if button==self.page_add then
            self.page = math.min(self.page + 1,self.max_page)
            self:update()
        elseif button==self.page_los then
            self.page = math.max(self.page - 1,1)
            self:update()
        elseif button==self.esc then
            self:hide()
        elseif button.class=='菜单按钮' then
            self.show_data = button.map_data
            if self.select_map then
                self.select_map.title:set_color(0xffffffff)
            end
            self.select_map = button
            self.select_map.title:set_color(0xff00ff00)
            self.page = 1
            self:update()
        elseif button.class=='存档按钮' then
            local data = button.skill
            if data then
                if data.skills then
                    self.show_data = data.skills
                    self.page = 1
                    self:update()
                elseif data.model then
                    local open = data.open or player:get_data('激活-'..data.name)
                    if open then
                        ac.sync('同步-装扮模型',data.model,data.ref)
                    end
                end
            end
        end
    end,
    on_button_mouse_enter = function(self,button)
        local skill = button.skill
        local player = ac.player.self
        if skill and not skill.is_empty then
            local tip = skill.tip
            local open = skill.open or player:get_data('激活-'..skill.name)
            if not open and skill.dis_tip then
                tip = ('需要：|n%s|n|n%s'):format(skill.dis_tip,skill.tip)
            end
            button.art = skill.art
            button:tooltip(skill.title or skill.name,tip,'bottom')
        end
    end,
    open = function(self)
        if self.show_flag then
            self:hide()
        else
            self:show()
        end
    end
}


--local maps = {'特殊福利','等级存档','通关存档','装扮存档','商城道具'}
--local ui = class.save_ui.create('存档',(1920 - 750)/2,200,750,450,{
--    w = 64,
--    h = 64,
--    g1 = 5,
--    g2 = 5,
--})
--ac.wait(100,function()
--    for _,name in ipairs(maps) do
--        ui:add_map(name,get_dz_data(name))
--    end
--end)
--
--local button = class.button.create([[ui\面板\存档UI.tga]],390,40,80,80)
--button.keys = {'F5','ESC'}
--function button:on_button_key_down(key)
--    if key=='ESC' then
--        ui:hide()
--    else
--        ui:open()
--    end
--end
--
--function button:on_button_clicked()
--    ui:open()
--end
